<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Loader;

class Notify extends Controller
{

    public function alipayTour(){

        Loader::import('Alipay.aop.AopClient');
        $aop = new \AopClient();
        $config = config('alipay');

        $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];

        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        $noticeData = $_POST;

        if ($flag){

            if ($noticeData['trade_status'] == 'TRADE_SUCCESS' || $noticeData['trade_status'] == 'TRADE_FINISHED'){

                $orderInfo = Db::name('tour_order')->where("order_sn",$noticeData['out_trade_no'])->find();

                if (!empty($orderInfo)){
                    if ($noticeData['total_amount']*1 == $orderInfo['order_price']*1 && $noticeData['seller_id'] == '2088631895391944' && $noticeData['app_id'] == '2019120969709820') {

                        if ($orderInfo['is_pay'] == 1){
                            echo "success";
                        }else{

                            //将订单修改为已支付
                            $editData['is_pay']       = 1;
                            $editData['pay_type']     = 1;
                            $editData['order_unique'] = $noticeData['trade_no'];
                            $editRes = Db::name('tour_order')->where("order_id",$orderInfo['order_id'])->update($editData);

                            if ($editRes){
                                echo 'success';
                            }else{
                                echo "failure";
                            }
                        }
                    }
                }
            }
        }else{
            echo "failure";
        }


    }

    public function alipayHotel(){

        Loader::import('Alipay.aop.AopClient');
        $aop = new \AopClient();
        $config = config('alipay');

        $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];

        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        $noticeData = $_POST;

        if ($flag){

            if ($noticeData['trade_status'] == 'TRADE_SUCCESS' || $noticeData['trade_status'] == 'TRADE_FINISHED'){

                $orderInfo = Db::name('hotel_order')->where("order_sn",$noticeData['out_trade_no'])->find();

                if (!empty($orderInfo)){
                    if ($noticeData['total_amount']*1 == $orderInfo['order_price']*1 && $noticeData['seller_id'] == '2088631895391944' && $noticeData['app_id'] == '2019120969709820') {

                        if ($orderInfo['is_pay'] == 1){
                            echo "success";
                        }else{

                            //将订单修改为已支付
                            $editData['is_pay']       = 1;
                            $editData['pay_type']     = 1;
                            $editData['order_unique'] = $noticeData['trade_no'];
                            $editRes = Db::name('hotel_order')->where("order_id",$orderInfo['order_id'])->update($editData);

                            if ($editRes){

                                echo 'success';

                            }else{

                                echo "failure";

                            }
                        }
                    }
                }
            }
        }else{
            echo "failure";
        }


    }

}